package com.newdream.ribbondemo.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.newdream.ribbondemo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ author: sanglei
 * @ date: 2019-04-30 10:13
 * @ description:
 */
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate template;

    @GetMapping("index")
    @HystrixCommand(fallbackMethod = "fallBack",
            threadPoolProperties = {@HystrixProperty(name = "coreSize", value = "1"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "1")},
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2")})
    public Object index(@RequestParam("id") Integer id) {
        //return template.getForObject("http://localhost:8004/get?id="+id,Object.class);
        return template.getForObject("http://eurekaclient/get?id=" + id, Object.class);
    }

    @GetMapping("getUser")
    public Object getUser() {
        return template.getForEntity("", User.class);
    }

    public Object fallBack() {
        return "超时降级！";
    }
}
